import { Indicator, MessageBox } from 'mint-ui'

// 组件异步加载优化
export function asyncLoad(loadComponent, delay = 300) {
  return async function() {
    let loading = true
    setTimeout(() => {
      if (loading) Indicator.open('加载中...')
    }, delay)
    try {
      const component = await loadComponent()
      // await sleep()
      loading = false
      Indicator.close()
      return component
    } catch (err) {
      loading = false
      MessageBox.alert('加载失败', '提示')
      throw err
    }
  }
}

export function sleep(delay = 1000) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve()
    }, delay)
  })
}
